package com.intelli.eve;

import com.intelli.eve.core.api.common.Priority;
import com.intelli.eve.core.api.message.Header;
import com.intelli.eve.core.impl.io.local.ComponentInvocationInfo;
import com.intelli.eve.core.impl.io.local.SystemMessage;
import com.intelli.eve.core.impl.message_bus.MessageBus;
import com.intelli.eve.core.impl.threads.BackgroundTaskManager;
import com.intelli.eve.core.meta.ContextListener;
import com.intelli.ray.core.AnnotationBasedContext;
import com.intelli.ray.core.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Sergey42
 * Date: 14.11.13 21:04
 */
public class Eve {

    private static final List<ContextListener> contextListeners = new ArrayList<>();
    private static Context context;

    public static void addListener(ContextListener contextListener) {
        contextListeners.add(contextListener);
    }

    public static Context getContext() {
        if (context == null) throw new IllegalStateException("Context isn't initialized yet");
        return context;
    }

    public static void main(String[] args) throws Exception {
        context = new AnnotationBasedContext("com.intelli.eve");
        context.printConfiguredBeans(System.out);
        for (ContextListener contextListener : contextListeners) {
            contextListener.onStart();
        }

        BackgroundTaskManager backgroundTaskManager = context.getBean(BackgroundTaskManager.class);
        backgroundTaskManager.submitTask(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello");
            }
        });

        MessageBus bus = context.getBean(MessageBus.class);
        bus.add(new SystemMessage(new Header(1234, Priority.HIGH), new ComponentInvocationInfo(
                BackgroundTaskManager.NAME,
                "submitTask", new Class[]{Runnable.class},
                new Object[]{new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("Hello again");
                    }
                }})));
    }
}
